<template>
	<view>
		<view class="wanl-product">
			<view class="product_warter" style="padding-top: unset;" :class="dataStyle"
				v-if="dataStyle == 'col-2-10' || dataStyle == 'col-2-15' || dataStyle == 'col-2-20' || dataStyle == 'col-2-25' || dataStyle == 'col-2-30'">

				<!-- 首页-商品分类-活动-横向 -->
				<view class="product-activity margin-bottom-sm" @tap="toActivity(activity)"
					v-if="column == 'horizontal'">
					<!-- <view class="img-bg normal-img" style="height: 340rpx;"
						:style="{ background: `url(${$wanlshop.oss(activity.img_url)})` }">
					</view> -->
					<image :src="$wanlshop.oss(activity.img_url)" mode="widthFix"></image>
				</view>

				<view class="wanl-waterfall">
					<view id="wanl-left-cloumn" class="wanl-cloumn">

						<view class="warter left" v-for="(item, index) in leftList" :key="index">
							<view>
								<!-- 首页-商品分类-活动-纵向 -->
								<block v-if="item == 'vertical'">
									<view class="product-activity" style="margin-bottom: 0;"
										 @tap="toActivity(item)">
										<image :src="$wanlshop.oss($wanlshop.oss(activity.img_url))" mode="widthFix">
										</image>
									</view>
								</block>

								<view v-else @tap="handleGoods(item.id)">
									<image class="image product-image" :src="$wanlshop.oss(item.show_images[0])"
										mode="aspectFill">
									</image>
									<!-- <view class="img-bg product-image"
											:style="{backgroundImage: 'url(' + $wanlshop.oss(item.show_images[0]) + ')'}"></view> -->
									<view class="content padding-bj">
										<!-- 卡片底部内容区域 -->
										<view class="publisher flex align-center">
											<view class="img-bg avatar"
												:style="{ backgroundImage: `url(${$wanlshop.oss(item.pet_avatar)})` }">
											</view>
											<view class="pet-name">{{ item.pet_name }}</view>
										</view>
										<scroll-view :scroll-x="true">
											<view class="tag-list">
												<view class="tag" v-for="(tag, index) in item.tag" :key="index">
													{{ tag }}
												</view>
											</view>
										</scroll-view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view id="wanl-right-cloumn" class="wanl-cloumn">
						<view class="warter right" v-for="(item, index) in rightList" :key="index"
							@tap="handleGoods(item.id)">

							<image class="image product-image" :src="$wanlshop.oss(item.show_images[0])"
								mode="aspectFill">
							</image>

							<view class="content padding-bj">
								<!-- 卡片底部内容区域 -->
								<view class="publisher">
									<view class="img-bg avatar"
										:style="{ backgroundImage: `url(${$wanlshop.oss(item.pet_avatar)})` }">
									</view>
									<view class="pet-name">{{ item.pet_name }}</view>
								</view>
								<view class="">
								</view>
								<scroll-view :scroll-x="true">
									<view class="tag-list">
										<view class="tag" v-for="(tag, index) in item.tag" :key="index">
											{{ tag }}
										</view>
									</view>
								</scroll-view>
							</view>

						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'index-adopt',
		props: {
			activity: {
				type: Object,
				default: function() {
					return {};
				}
			},
			dataList: {
				type: Array,
				required: true,
				default: function() {
					return [];
				}
			},
			dataStyle: {
				type: String,
				default: 'col-2-20'
			}
		},
		data() {
			return {
				addTime: 180,
				leftList: [],
				rightList: [],
				tempList: [],
				children: [],
				wanlsys: {},
				anchorlist: [{
					name: '主图',
					top: 0
				}],
			}
		},
		watch: {
			copyFlowList(nVal, oVal) {
				// 取差值，即这一次数组变化新增的部分
				let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
				if (nVal.slice(startIndex).length == 0) {
					this.leftList = [];
					this.rightList = [];
					this.tempList = this.cloneData(this.copyFlowList);
				} else {
					this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
				}
				this.splitData();
			},
		},
		created() {
			this.wanlsys = this.$wanlshop.wanlsys();
		},
		mounted() {
			this.tempList = this.cloneData(this.copyFlowList);
			this.splitData();
		},
		computed: {
			// 破坏flowList变量的引用，否则watch的结果新旧值是一样的
			copyFlowList() {
				return this.cloneData(this.dataList.reverse());
			},
			// 活动图方向
			column() {
				return this.activity ? this.activity.column : '';
			}
		},
		methods: {
			// 前往活动页
			toActivity(item) {
				this.$api.post({
					loadingTip: '加载中',
					url: `/wanlshop/activity/detail`,
					data: {
						type: 'category',
						type_id: item.category_id
					},
					success: result => {
						uni.navigateTo({
							url: `/pages/page/activityPage`,
							success: (res) => {
								res.eventChannel.emit('data', {
									activity: result?.title,
									productList: result?.categories || [],
									headerImage: result?.header_img,
									footerImage: result?.footer_img,
								})
							}
						})
					}
				})
			},
			async splitData() {
				if (!this.tempList.length) return;

				let leftRect = await this.getRect('#wanl-left-cloumn');
				let rightRect = await this.getRect('#wanl-right-cloumn');
				// console.log('leftReact', leftRect.height)
				// console.log('rightRect', rightRect.height)

				// 如果左边小于或等于右边，就添加到左边，否则添加到右边
				let item = this.tempList[0];
				if (!item) return;

				item.comment = this.$wanlshop.toFormat(item.comment, 'hundred');
				item.praise = item.comment > 0 ? parseInt((item.praise / item.comment) * 100) : 0;

				// 插入活动卡片
				if (this.column == 'vertical' && this.leftList.length == 0) {
					this.leftList.push('vertical')
				}

				if (leftRect.height < rightRect.height) {
					// console.log('left')
					this.leftList.push(item);
				} else if (leftRect.height > rightRect.height) {
					// console.log('right')
					this.rightList.push(item);
				} else {

					// 这里是为了保证第一和第二张添加时，左右都能有内容
					// 因为添加第一张，实际队列的高度可能还是0，这时需要根据队列元素长度判断下一个该放哪边
					if (this.leftList.length <= this.rightList.length) {
						this.leftList.push(item);
					} else {
						this.rightList.push(item);
					}
				}
				// 移除临时列表的第一项
				this.tempList.splice(0, 1);
				// 如果临时数组还有数据，继续循环
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime)
				}
			},
			// 复制而不是引用对象和数组
			cloneData(data) {
				return JSON.parse(JSON.stringify(data));
			},
			getRect(selector, all) {
				return new Promise(resolve => {
					let query = null;

					query = uni.createSelectorQuery().in(this)

					//setTimeout(() => {
						query[all ? 'selectAll' : 'select'](selector)
							.boundingClientRect(rect => {
								if (all && Array.isArray(rect) && rect.length) {
									resolve(rect)
								}
								if (!all && rect) {
									resolve(rect)
								}
							})
							.exec()
					//}, 500)
				})
			},
			handleGoods(id) {
				this.$wanlshop.to(`/pages/adopt/details?id=${id}`)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wanl-product {
		background-color: transparent;
	}

	.wanl-waterfall {
		display: flex;
		flex-direction: row;
		align-items: flex-start;



		.wanl-cloumn {
			display: flex;
			flex: 1;
			flex-direction: column;
			height: auto;
			width: 50%;

			.warter {
				// box-shadow: 0px 0px 6rpx 0px rgba(0, 0, 0, 0.2);
				border: 2rpx solid #dfdfdf;
			}

			.wanl-image {
				width: 100%;
			}
		}

		.content {
			.publisher {
				gap: 6rpx;
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;

				.avatar {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					flex-shrink: 0;
					background-size: cover;
				}
			}

			.pet-name {
				font-size: 28rpx;
				font-weight: bold;
				width: 300rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			.tag-list {
				display: flex;
				flex-wrap: nowrap;
				gap: 10rpx 15rpx
			}

			.tag {
				background-color: #F2F2F2;
				color: #5C3734;
				padding: 4rpx 10rpx;
				border-radius: 8rpx;
				font-size: 24rpx;
				width: fit-content;
				border: 2rpx solid #846966;
				white-space: nowrap;
			}
		}
	}

	.product-activity {
		border-radius: 20rpx;
		overflow: hidden;
		// box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2);

		image {
			width: 100%;
			display: block;
		}
	}

	.product-image {
		width: 100%;
		//min-height: 340rpx;
		min-height: 340rpx;
		max-height: 340rpx;
	}

	.normal-img {
		width: 100%;
		height: 340rpx !important;
		background-size: cover !important;
		background-position: center !important;
	}
</style>